package com.hsy.swu.hsyutils

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.viewbinding.ViewBinding

abstract class HsyActivity<VB : ViewBinding, VM : HsyViewModel> : AppCompatActivity() {
    protected val mBinding by lazy {
        createViewBinding()!!
    }

    protected val mViewModel by lazy {
        createViewModel()
    }

    private val logName = javaClass.simpleName
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT // 禁用屏幕旋转，始终纵向
        Log.v(TAG, "$logName onCreate")
        enableEdgeToEdge()
        setContentView(mBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(mBinding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    override fun onStart() {
        super.onStart()
        Log.v(TAG, "$logName onStart")
        mBinding.setFont()

    }

    override fun onResume() {
        super.onResume()
        Log.v(TAG, "$logName onResume")

    }

    override fun onPause() {
        super.onPause()
        Log.v(TAG, "$logName onPause")

    }

    override fun onStop() {
        super.onStop()
        Log.v(TAG, "$logName onStop")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.v(TAG, "$logName onDestroy")

    }

    override fun onRestart() {
        super.onRestart()
        Log.v(TAG, "$logName onRestart")

    }

    /**
     * 通过反射创建 viewBinding
     * @return  ViewBinding
     */
    open fun createViewBinding(): VB? {
        return createViewBinding(layoutInflater)
    }

    /**
     * 通过反射创建 viewModel
     * @return    ViewModel
     */
    open fun createViewModel(): VM {
        return createViewModel(this)
    }
}